//
//  WBStatusCell.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/13.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit
import SnapKit

class WBStatusCell: UITableViewCell {
    
    var bottomViewTopConstraint: Constraint?
    
    var statusViewModel: WBStatusViewModel?{
    
        didSet{
        
            //给上面的视图赋值数据
            topView.statusViewModel = statusViewModel
            
            //给中间的视图赋值数据 如果retweeted_status == nil 就说明没有转发微博
            //这个时候底部视图上边就要与原文底边一起 
            //！= nil 就说明有转发微博
            
            //01 卸载约束
            bottomViewTopConstraint?.uninstall()
            
            if statusViewModel?.statusModel?.retweeted_status == nil{
                //没有转发微博
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) in
                    //记录下该约束 cell重用的时候要卸载
                  bottomViewTopConstraint = make.top.equalTo(topView.snp_bottom).constraint
                })
                
                //隐藏转发微博
                middleView.hidden = true
            
            }else
            {
                //有转发微博
                //赋值
                middleView.statusViewModel = statusViewModel
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) in
                    
                    bottomViewTopConstraint = make.top.equalTo(middleView.snp_bottom).constraint
                })
                //显示转发微博
                middleView.hidden = false
            }
            
            bottomView.statusViewModel = statusViewModel
        
        }
    
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }
    
    //设置UI
    func setupUI() {
        
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        //添加控件
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(middleView)
        
        //设置约束
        topView.snp_makeConstraints { (make) in
            //上左右距离0 高度50
            make.leading.trailing.equalTo(contentView)
            //设置线条
            make.top.equalTo(contentView).offset(8)
            //高度在自己的view里面设置
            //make.height.equalTo(50)
        }
        
        middleView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom)
        }
        
        bottomView.snp_makeConstraints { (make) in
            //记录这个约束 以后修改的时候用
            bottomViewTopConstraint = make.top.equalTo(middleView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
            
        }
        
        contentView.snp_makeConstraints { (make) in
           
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        
    }
  
    
    //懒加载控件  分三个视图 上 中 下
    lazy var topView:WBTopView = WBTopView()
    lazy var middleView:WBMiddleView = WBMiddleView()
    lazy var bottomView:WBBottomView = WBBottomView()

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
